<template>
  <div class="list-table" style="overflow: auto">
    <el-form ref="queryForm" :model="queryParams" :inline="true" style="position: unset !important;height: 50px;overflow: hidden;">
      <el-form-item :label="$t('创建人')" prop="createBy">
        <userSelect
          v-model="queryParams.createBy"
          size="small"
          :placeholder="$t('申请人')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('生成时间')" prop="createTime">
        <DatePicker
          v-model="dateRange"
          type="daterange"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
        <el-button v-hasPermi="['deal:analysisValuation:edit']" type="primary" icon="el-icon-plus" size="mini" @click="addHandle">{{ $t('新增') }}</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="dealAnalysisValuationReportList" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('创建人')" align="center" prop="createBy.nickName" />
      <el-table-column :label="$t('生成时间')" align="center" prop="createTime">
        <template slot-scope="scope">
          {{ parseTime(scope.row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('生成文件')" align="center" prop="fileName">
        <template slot-scope="scope">
          <span v-if="isEmpty(scope.row.fileId)">{{ $t('生成中') }}...</span>
          <el-link v-else type="primary" :underline="false" @click="onlinePreview(scope.row)">{{ scope.row.fileName }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            :disabled="isEmpty(scope.row.fileId)"
            @click="downloadFile(scope.row.fileId)"
          >{{ $t('下载') }}</el-button>
          <el-button
            v-hasPermi="['deal:analysisValuation:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改估值报告对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" width="400px" style="height: 300px;top: 20vh;" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealAnalysisValuationReportForm ref="dealAnalysisValuationReportForm" />
    </el-dialog>

    <!-- 预览附件 -->
    <OnlinePreview ref="OnlinePreview" />
  </div>
</template>

<script>
import {
  listDealAnalysisValuationReport,
  delDealAnalysisValuationReport
} from '@/api/deal/analysisValuation/dealAnalysisValuationReport'
import dealAnalysisValuationReportForm from './dealAnalysisValuationReportForm.vue'
import { downLoadFileOpen } from '@/utils/zipdownload'

export default {
  components: {
    dealAnalysisValuationReportForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 估值报告表格数据
      dealAnalysisValuationReportList: [],
      // 弹出层标题
      title: '估值报告',
      // 是否显示弹出层
      formOpen: false,
      // 查询参数
      queryParams: {
        createBy: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      dateRange: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询估值报告列表 */
    getList() {
      this.loading = true
      listDealAnalysisValuationReport(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.dealAnalysisValuationReportList = response.rows
        this.total = response.total
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        fileId: undefined,
        createTime: undefined,
        createBy: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.dateRange = undefined
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.dealAnalysisValuationReportForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.dealAnalysisValuationReportForm.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(() => {
        return delDealAnalysisValuationReport(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(() => {
        this.loading = false
      })
    },
    /** 预览 **/
    // 预览附件
    onlinePreview(row) {
      this.$refs.OnlinePreview.onlinePreviewFile(row.fileId, 'view')
    },
    /** 下载 **/
    downloadFile(fileId) {
      downLoadFileOpen(fileId)
    }
  }
}
</script>
